<template>
  <div>
    <el-card>
      <template #header>管理员信息</template>
      <div>
        <el-row>
          <el-col>管理员ID:</el-col>
          <el-col>{{ arr.id }}</el-col>
        </el-row>
        <el-row>
          <el-col>账号:</el-col>
          <el-col>{{ arr.account }}</el-col>
        </el-row>
        <el-row>
          <el-col>超级管理员:</el-col>
          <el-col>{{ arr.userGroup }}</el-col>
        </el-row>
        <el-row>
          <el-col>创建时间:</el-col>
          <el-col>{{ arr.ctime | format }}</el-col>
        </el-row>
        <el-row>
          <el-col>管理员头像:</el-col>
          <el-col>
            <el-upload
              class="avatar-uploader"
              action="http://127.0.0.1:5000/users/avatar_upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-col>
        </el-row>
        <el-row>
          <el-col> </el-col>
          <el-col>
            <el-button type="primary" @click="amend">保存头像</el-button>
          </el-col>
        </el-row>
      </div>
    </el-card>
  </div>
</template>

<script>
import local from '@/utils/local.js'
import { amendimg } from '@/api/user.js'
import moment from 'moment'
export default {
  data() {
    return {
      imageUrl: '',
      arr: {},
      str: ''
    }
  },
  created() {
    this.personal()
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.str = res.imgUrl
    },
    beforeAvatarUpload(file) {
      const arr = ['image/jpeg', 'image/jpg', 'image/gif']
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!arr.includes(file.type)) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return arr && isLt2M
    },
    // 个人中心
    personal() {
      const ar = JSON.parse(local.get('user'))
      this.arr = ar
    },
    // 修改头像
    async amend() {
      const { code, msg } = await amendimg({
        imgUrl: this.str
      })
      if (code === 0) {
        location.reload()
        this.$message.success(msg)
      } else {
        this.$message.error(msg)
      }
    }
  },
  filters: {
    format(str) {
      return moment(str).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>

<style lang="less" scoped>
.el-row {
  display: flex;
  justify-content: flex-start;
  padding: 15px 10px;
  border-bottom: 1px solid #e4e4e4;
  .el-col:nth-of-type(1) {
    width: 200px;
  }
}
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
/deep/.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 128px;
  height: 128px;
  line-height: 128px;
  text-align: center;
}
.avatar {
  width: 128px;
  height: 128px;
  display: block;
}
</style>
